package com.park.web.controller.admin;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.user.UserModel;
import com.park.client.model.user.UserMsgModel;
import com.park.client.model.user.UserMsgStatusModel;
import com.park.client.service.user.UserMsgService;
import com.park.client.service.user.UserMsgStatusService;
import com.park.client.service.user.UserService;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;
import com.park.web.util.SessionUtil;

@Controller
@RequestMapping("/admin/msg")
public class AdminMsgController extends BaseController{

	@Autowired
	private UserMsgService userMsgService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserMsgStatusService userMsgStatusService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<UserMsgModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<UserMsgModel>  pageData = new PageData<>();
		UserMsgModel userMsg = new UserMsgModel();
		Integer total = userMsgService.selectTotal(userMsg);
		Page page = new Page(pageNow,total,rows);
		userMsg.setStartPos(page.getStartPos());
		userMsg.setPageSize(page.getPageSize());
		List<UserMsgModel> list = userMsgService.selectPagination(userMsg);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	@RequestMapping("msgList")
	public String newsList(HttpServletRequest request){
			return Global.ADMIN_PATH+"msg/msgList";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Result<Boolean> save(UserMsgModel msg,HttpServletRequest request,HttpSession session){
		Result<Boolean> json = new Result<>();
		msg.setCreateDate(new Date());
		msg.setCreater(SessionUtil.getUserID(session));
		boolean success = userMsgService.insertAndGetInsertID(msg);
		if(success){
			json.setMessage("添加成功!");
			json.setSuccess(true);
			//给所有用户发消息
			List<UserModel> list = userService.selectAll();
			for (UserModel user : list) {
				UserMsgStatusModel userMsg = new UserMsgStatusModel();
				userMsg.setStatus(0);
				userMsg.setCreater(user.getId());
				userMsg.setMsgId(msg.getId());
				userMsgStatusService.insert(userMsg);
			}
		}else{
			json.setSuccess(false);
			json.setMessage("添加失败!");
		}
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<Boolean> deleteById(@RequestParam("id") Long id,HttpServletRequest request){
		Result<Boolean> json = new Result<>();
		boolean success = userMsgService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage("删除成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("删除失败!");
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Result<Boolean> edit(UserMsgModel msg,HttpServletRequest request){
		Result<Boolean> json = new Result<>();
		boolean success = userMsgService.updateByPrimaryKeySelective(msg);
		if(success){
			json.setMessage("修改成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("修改失败!");
			json.setSuccess(false);
		}
		return json;
	}
}
